package com.ensah.web.actions;



import java.awt.Color;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.util.Base64;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.struts2.ServletActionContext;
import org.xhtmlrenderer.pdf.ITextRenderer;


import com.boudaa.dao.exceptions.EntityNotFoundException;

import com.ensah.metier.bo.Attestation;
import com.ensah.metier.bo.Classe;

import com.ensah.metier.bo.Etudiant;

import com.ensah.service.AttestationService;
import com.ensah.service.ClasseService;

import com.ensah.service.EtudiantService;


import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.opensymphony.xwork2.ActionSupport;

public class AttestationAction extends ActionSupport{
	
	
	private String cne;
	private boolean etat;
	private Date date;

	private Long idAt;
	private ClasseService c;
	
	
	private List<Attestation> listeAtt;
	
	private EtudiantService etd;

	private AttestationService at;


	
	public String ajouterAtt(){
		
		date=new Date();
		etat=false;
		Etudiant e;
		
		
		try {
			e = etd.getEtudiantByCne(cne);
			Attestation att=new Attestation(etat, date, e);
			
			listeAtt=at.getListeAttByCne(cne);
			
			listeAtt.add(att);
			 e.setAttestations(listeAtt);
			 at.saveAttestation(att);
			 etd.modifierEtudiant(e);
			
		} catch (EntityNotFoundException e1) {
			
			
			return "error";
		}
		
		return "success";
		
		
	}


	public String supprimerAtt(){
		
	
		System.out.println("fdgfdf"+idAt);
		at.deleteAttestation(idAt);
		
		return "success";
		
		
	}
	
	
	public String generer() throws EntityNotFoundException{

		cne = ServletActionContext.getRequest().getParameter("cne");

		System.out.println("cnecnecne"+cne);

		
		return SUCCESS;
		
	}
public void genererAtt() {

	
		Etudiant e;
		try {
			e = etd.getEtudiantByCne(cne);
			
			Classe classe=c.getClasseById(e.getClasse().getIdClasse());
			 String nomClasse=classe.getNom();
				
			 
			 HttpServletResponse response = ServletActionContext.getResponse();
			    Document document = new Document();
				URL url=new URL("http://localhost:8080/GsAbsence/att");
			     Reader htmlreader = new BufferedReader(new InputStreamReader(
			                              url.openStream())
			                             );
			     ByteArrayOutputStream buffer = new ByteArrayOutputStream();
			     PdfWriter.getInstance(document, buffer);
			        document.open();
			       
			      StyleSheet styles = new StyleSheet();
			      
			     
			      styles.loadTagStyle("h1", "align", "center");
			      styles.loadStyle("lieu", "align", "right");
			      styles.loadTagStyle("h1", "font", "EucrosiaUPC");
			      styles.loadTagStyle("h1", "size", "3");
			      
			      ArrayList arrayElementList = HTMLWorker.parseToList(htmlreader, styles);
			      for (int i = 0; i < arrayElementList.size(); ++i) {
			          Element el = (Element) arrayElementList.get(i);
			          document.add(el);
			      }
			      document.close();
			        
			        Attestation att=at.getAttestationById(idAt);
			        att.setEtat(true);
			        at.updateAttestation(att);
			        ServletOutputStream outputStream = response.getOutputStream() ;
				    buffer.writeTo(outputStream);
				    response.setHeader("Content-Disposition", "attachment; filename=\"stuReport.pdf\"");
				    response.setContentType("application/pdf");
				    outputStream.flush();
				    outputStream.close();
				    }
			   catch (DocumentException ex) {
				 
			   } catch (EntityNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			    	
			   



	}

public String listAtt(){
	
	listeAtt=at.getListeAtt();
	
	return "success";
	
	
	
}
	


	public String getCne() {
		return cne;
	}

	public void setCne(String cne) {
		this.cne = cne;
	}


	public List<Attestation> getListeAtt() {
		listeAtt=at.getListeAtt();

		return listeAtt;
	}

	public void setListeAtt(List<Attestation> listeAtt) {
		this.listeAtt = listeAtt;
	}

	public AttestationService getAt() {
		
		return at;
	}

	public void setAt(AttestationService at) {
		this.at = at;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
	public boolean isEtat() {
		return etat;
	}

	public void setEtat(boolean etat) {
		this.etat = etat;
	}



	public EtudiantService getEtd() {
		return etd;
	}
	public void setEtd(EtudiantService etd) {
		this.etd = etd;
	}

	
	public ClasseService getC() {
		return c;
	}

	public void setC(ClasseService c) {
		this.c = c;
	}
	private InputStream inputStream;
	
	public InputStream getInputStream() {
	    return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
	    this.inputStream = inputStream;
	}
	
	public Long getIdAt() {
		return idAt;
	}


	public void setIdAt(Long idAtt) {
		this.idAt = idAtt;
	}

}
